package com.nju.bean.client;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import android.util.Log;

import com.nju.bean.client.string.XmlHandler;
import com.nju.bean.client.string.XmlMaker;

public class SubmitManager extends NetManager{
	
	
	private ArrayList<String> tittleList;
	private String topicId;
	private String fragmentId;
	
	public SubmitManager(){
		ipAdress = "192.168.0.1";
		port = 8001;
	}
	
	public SubmitManager(String ip,int port){
		ipAdress = ip;
		this.port = port;
	}
	private String post;
	public void postFragment(String author,String text,double longitude,double latitude,boolean containImg) throws UnknownHostException, IOException{
		post = XmlMaker.makePost(author, text, longitude, latitude,containImg);
		socket = new Socket(ipAdress,port);
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(),"GB2312"));
		post = escapeChar(post);
		bw.write(post);
		System.out.println(post);
		bw.flush();
		BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream(),"GB2312"));
		String line = br.readLine();
		String feedback = new String();
		while(line!=null){
			feedback+=line;
			line = br.readLine();
		}
		XmlHandler xh = new XmlHandler(feedback);
		
		tittleList = xh.getTextArray("tittle");
		topicId = xh.getAttribute("choicedtittle", "topicid");
		this.fragmentId = xh.getAttribute("choicedtittle","fragmentId");
		bw.close();
		br.close();
	}
	
	public void submitTittle(String id,String tittle) throws IOException{
		String choicedTittle =  XmlMaker.makeChoiceTittle(id, tittle);
		socket = new Socket(ipAdress,port);
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(),"GB2312"));
		choicedTittle = escapeChar(choicedTittle);
		bw.write(choicedTittle);
		bw.flush();
		bw.close();
		socket.close();
	}
	
	public String getTopicId(){
		return this.topicId;
	}
	
	public String getFragmentId(){
		return fragmentId;
	}
	
	public String getPost(){
		return post;
	}
	public ArrayList<String> getChoicedTittle(){
		return tittleList;
	}
	
	public boolean postDiscuzz(String newsId,String author,String reply) throws UnknownHostException, IOException{
		String postReply = XmlMaker.makeReply(newsId, author, reply);
		socket = new Socket(ipAdress,port);
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(),"GB2312"));
		postReply = escapeChar(postReply);
		bw.write(postReply);
		bw.flush();
		bw.close();
		socket.close();
		return true;
	}
	
	public boolean plusFragment(String fragmentId,String userId)throws UnknownHostException, IOException{
		String plus = XmlMaker.makeScoreToFragment(fragmentId, userId, 1);
		sendMessage(plus);
		return true;
	}
	
	public boolean minuteFragment(String fragmentId,String userId)throws UnknownHostException, IOException{
		String minute = XmlMaker.makeScoreToFragment(fragmentId, userId, -1);
		sendMessage(minute);
		return true;
	}
	
	public boolean sendUserInfo(String name,String secret,String token)throws UnknownHostException, IOException{
		String info = XmlMaker.makeUserInfo(name, secret, token);
		socket = new Socket(ipAdress,port);
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(),"GB2312"));
		info = escapeChar(info);
		bw.write(info);
		bw.flush();
		String result = readFromSocket();
		XmlHandler xh = new XmlHandler(result);
		String bool = xh.getText("result");
		Log.v("feedback", bool);
		bw.close();
		return bool.equals("success");
	}
	
	private boolean sendMessage(String str) throws UnknownHostException, IOException{
		socket = new Socket(ipAdress,port);
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(),"GB2312"));
		str = escapeChar(str);
		bw.write(str);
		bw.flush();
		bw.close();
		socket.close();
		return true;
	}
	
	private String readFromSocket()throws UnknownHostException, IOException{
		Log.v("readFromSocket", "start");
		BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream(),"GB2312"));
		String line = br.readLine();
		Log.v("readFromSocket",line);
		String feedback = new String();
		while(line!=null){
			feedback+=line;
			line = br.readLine();
		}
		return feedback;
	}
	
	private String escapeChar(String str){
		String s;
		s = str;//.replace("'", "''");
		return s;
	}
}